package com.technology.education.ui.common.preview_photo

import android.os.Bundle
import android.view.View
import androidx.viewpager2.widget.ViewPager2
import com.technology.education.data.PhotoString
import com.technology.education.data.SpKeys
import com.technology.education.databinding.ActivityPreviewPhotoBinding
import com.technology.education.ext.postEventBus
import com.technology.education.widget.PreviewPhotosAdapter
import com.zhpan.bannerview.BannerViewPager
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import java.net.URLDecoder

/**
 *@author:create by BZY
 *Date: 2020/9/18 09
 * 预览图片
 */
class PreviewPhotoActivity : BaseVmDbActivity<BaseViewModel, ActivityPreviewPhotoBinding>() {
    var pos = 0
    var currentPos = 0
    var canDel = false
    var photos = arrayListOf<String>()
    private var photoAdapter: PreviewPhotosAdapter? = null
    override fun initView(savedInstanceState: Bundle?) {
        intent.apply {
            pos = getIntExtra("pos", 0)
            currentPos = pos
            canDel = false
            photos = getStringArrayListExtra("photos") as ArrayList<String>

        }
        setData()
        setTopData()

        mDatabind.ivDel.setOnClickListener {
            postEventBus(SpKeys.DELETE_PHOTO_PATH, photos[currentPos])
            photos.removeAt(currentPos)
            (mDatabind.banner as BannerViewPager<String, *>).refreshData(photos)
            if (photos.isNullOrEmpty()) {
                onBackPressed()
            }
            setTopData()
        }
        mDatabind.tvBack.setOnClickListener {
            onBackPressed()
        }
        lazyLoadData()
    }

    private fun setData() {
        photoAdapter = PreviewPhotosAdapter()
        (mDatabind.banner as BannerViewPager<String, *>).apply {
            adapter = photoAdapter
            setAutoPlay(false)
            setCanLoop(false)
            setIndicatorVisibility(View.INVISIBLE)
            setOnPageClickListener {
                finish()
            }
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    currentPos = position
                    setTopData()
                }
            })
            currentItem = pos
        }.create(photos as List<String>?)
    }

    private fun setTopData() {
        mDatabind.tvBack.text = "${currentPos + 1}/${photos.size}"
        mDatabind.ivDel.visibility = if (canDel) View.VISIBLE else View.INVISIBLE
    }

    private fun lazyLoadData() {
        mDatabind.banner.postDelayed({
            (mDatabind.banner as BannerViewPager<*, *>).setCurrentItem(
                pos,
                false
            )
        }, 300)
    }

    override fun createObserver() {

    }

    override fun showLoading(message: String) {
    }

    override fun dismissLoading() {
    }

}